<template>
	<view>
		<z-nav-bar title="分享" transparentTitle="auto" :isBlack="false"></z-nav-bar>
		<view v-if="!types" class="text-c uni-white text-w uni-mb-32 z-font-28 uni-orange">分享到学员微信，完成学员绑定</view>
		<image :src="imgSrc" class="share-box" mode="heightFix"></image>
		<view class="buttons flex_cc">
			<view class="flex_cc button-common uni-orange-bg" @click="savePhotosAlbumModal">
				<uni-icons type="download-filled" size="20" color="#FFFFFF"></uni-icons>
				<view>保存相册</view>
			</view>
			<view class="flex_cc button-common uni-color-main-bg uni-ml-40" @click="sharePlacard">
				<uni-icons type="weixin" size="20" color="#FFFFFF"></uni-icons>
				<view>分享海报</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { getStuBindImg } from '@/api/player.js';
import { onLoad } from '@dcloudio/uni-app';
import { ref, reactive, getCurrentInstance } from 'vue';
const { proxy: that } = getCurrentInstance();

const playerInfo = uni.getStorageSync('player-info') || {};
const imgSrc = ref(''), types = ref(0)
onLoad(opt => {
	const type = parseInt(opt.type || 0)
	types.value = type
	if (type) {
		imgSrc.value = that.setting.oss_pub_domain + that.setting.app_share
	} else getStuBindImg(playerInfo.id).then(res => {
		imgSrc.value = res
	})
	
})

function savePhotosAlbumModal() {
	const saveShare = uni.getStorageSync('save-share'),
		{ osName } = uni.getSystemInfoSync();
	if (saveShare || osName === 'ios') savePhotosAlbum();
	else
		uni.showModal({
			title: '存储权限使用说明',
			content: '用于保存图片到相册等场景，是否允许使用？',
			confirmText: '允许',
			cancelText: '禁止',
			success: cc => {
				if (cc.confirm) {
					savePhotosAlbum();
					uni.setStorageSync('save-share', 1);
				} else that.tools.alert('保存失败')
				
			}
		});
}

function savePhotosAlbum() {
	uni.downloadFile({
		url: imgSrc.value,
		success: (res) => {
			uni.saveImageToPhotosAlbum({
				filePath: res.tempFilePath,
				success: () => {
					that.tools.alert('保存成功');
				}
			});
		}
	});
}

function sharePlacard() {
	// #ifdef APP-PLUS
	uni.share({
		provider: 'weixin',
		scene: 'WXSceneSession',
		type: 2,
		imageUrl: imgSrc.value,
		success: () => {
			that.tools.alert('分享成功')
		}
	});
	// #endif
}
</script>

<style lang="scss">
@import 'share.scss';
</style>
